from django.db import models
from django.contrib.auth.models import User


class CarbonFootprint(models.Model):
    """碳足迹记录表 - 用户的碳排放记录数据"""
    CATEGORY_CHOICES = [
        ('餐饮', '餐饮'), ('交通', '交通'), ('能源', '能源'), ('日常', '日常'), ('购物', '购物'), ('其他', '其他')
    ]
    footprint_id = models.AutoField(primary_key=True, verbose_name='记录唯一ID', db_comment='记录唯一ID')
    user = models.ForeignKey('auth.User', on_delete=models.CASCADE, db_column='user_id', verbose_name='记录所属用户', db_comment='记录所属用户', null=True, blank=True)
    category = models.CharField(max_length=50, choices=CATEGORY_CHOICES, verbose_name='AI解析后的大类(餐饮、交通等)', db_comment='AI解析后的大类(餐饮、交通等)')
    quantity = models.CharField(max_length=50, null=True, blank=True, verbose_name='AI解析后的量(350g)', db_comment='AI解析后的量(350g)')
    carbon_emission = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='碳排放数值(kg CO₂e)', db_comment='碳排放数值(kg CO₂e)')
    record_date = models.DateField(verbose_name='记录日期', db_comment='记录日期')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间', db_comment='创建时间')

    class Meta:
        db_table = 'xiangsu_carbon_footprints'
        db_table_comment = '碳足迹记录表-用户的碳排放记录数据'
        verbose_name = '碳足迹记录'
        verbose_name_plural = '碳足迹记录'
        ordering = ['-record_date']

    def __str__(self):
        return f'用户{self.user.id}的碳足迹记录{self.footprint_id}'


class CarbonFactor(models.Model):
    """碳排放系数库 - 各类物品/行为的碳排放计算系数"""
    CATEGORY_CHOICES = [
        ('餐饮', '餐饮'), ('交通', '交通'), ('能源', '能源'), ('日常', '日常'), ('购物', '购物'), ('其他', '其他')
    ]
    factor_id = models.AutoField(primary_key=True, verbose_name='系数唯一ID', db_comment='系数唯一ID')
    item_name = models.CharField(max_length=100, unique=True, verbose_name='物品/行为名称', db_comment='物品/行为名称')
    category = models.CharField(max_length=50, null=True, blank=True, choices=CATEGORY_CHOICES, verbose_name='所属大类', db_comment='所属大类')
    emission_per_unit = models.DecimalField(max_digits=10, decimal_places=4, verbose_name='每单位碳排放量', db_comment='每单位碳排放量')
    unit = models.CharField(max_length=20, verbose_name='单位(如:kg、km)', db_comment='单位(如:kg、km)')

    class Meta:
        db_table = 'xiangsu_carbon_factors'
        db_table_comment = '碳排放系数库-各类物品/行为的碳排放计算系数'
        verbose_name = '碳排放系数'
        verbose_name_plural = '碳排放系数'

    def __str__(self):
        return f'{self.item_name}({self.emission_per_unit}{self.unit})'


class KnowledgeLog(models.Model):
    """AI生成的知识补给日志"""
    log_id = models.AutoField(primary_key=True)
    user = models.ForeignKey('auth.User', on_delete=models.CASCADE, db_column='user_id', verbose_name='用户')
    horizon_days = models.IntegerField(default=7, verbose_name='时间范围天数')
    prompt_text = models.TextField(null=True, blank=True, verbose_name='提示词')
    items_json = models.JSONField(default=list, verbose_name='生成的知识条目')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='生成时间')

    class Meta:
        db_table = 'xiangsu_carbon_knowledge_logs'
        verbose_name = '知识补给日志'
        verbose_name_plural = '知识补给日志'
        ordering = ['-created_at']

    def __str__(self):
        return f'KnowledgeLog({self.user_id}, {self.horizon_days}d, {self.created_at})'
